package tms.service;

import java.util.ArrayList;
import java.util.List;

import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.util.GenericType;

import tms.model.Account;
import tms.model.Aircraft;
import tms.model.Booking;
import tms.model.Destination;
import tms.model.Flight;


public class ServiceHandler {

	private static String URL = "http://localhost:8080/rest/TravelService/";
	private static String DESTINATION_URL = URL + Destination.TABLE_NAME.toLowerCase();
	private static String ACCOUNT_URL = URL + Account.TABLE_NAME.toLowerCase();
	private static String BOOKING_URL = URL + Booking.tableName.toLowerCase(); 
	private static String FLIGHT_URL = URL + Flight.tableName.toLowerCase(); 
	private static String AIRCRAFT_URL = URL + Aircraft.TABLE_NAME.toLowerCase(); 

	

	public static boolean deleteDestination(Destination obj){
		ClientRequest request = new ClientRequest(DESTINATION_URL + "/delete");

		//IMPORTANT: put the object into request
		request.body("application/xml", obj);

		try {
			ClientResponse<Boolean> response = request.post(Boolean.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public static boolean saveDestination(Destination obj){
		ClientRequest request = new ClientRequest(DESTINATION_URL + "/save");

		//IMPORTANT: put the object into request
		request.body("application/xml", obj);

		try {
			ClientResponse<Boolean> response = request.post(Boolean.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public static Destination getFirstDestination(String where){
		ClientRequest request = new ClientRequest(DESTINATION_URL + "/first");
		request.body("application/xml", where);
		try {
			ClientResponse<Destination> response = request.post(Destination.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static List<Destination> getListDestination(String where){
		ClientRequest request = new ClientRequest(DESTINATION_URL + "/list");
		request.body("application/xml", where);
		try {
			ClientResponse<List<Destination>> response = request.post(new GenericType<List<Destination>>(){});
			return response.getEntity(new GenericType<List<Destination>>(){});
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Destination>();
		}
	}



	public static Account getFirstAccount(String where){
		ClientRequest request = new ClientRequest(ACCOUNT_URL + "/first");
		request.body("application/xml", where);
		try {
			ClientResponse<Account> response = request.post(Account.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static boolean deleteAccount(Account obj){
		ClientRequest request = new ClientRequest(ACCOUNT_URL + "/delete");

		//IMPORTANT: put the object into request
		request.body("application/xml", obj);

		try {
			ClientResponse<Boolean> response = request.post(Boolean.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public static boolean saveAccount(Account obj){
		ClientRequest request = new ClientRequest(ACCOUNT_URL + "/save");

		//IMPORTANT: put the object into request
		request.body("application/xml", obj);

		try {
			ClientResponse<Boolean> response = request.post(Boolean.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}


	public static List<Account> getListAccount(String where){
		ClientRequest request = new ClientRequest(ACCOUNT_URL + "/list");
		request.body("application/xml", where);
		try {
			ClientResponse<List<Account>> response = request.post(new GenericType<List<Account>>(){});
			return response.getEntity(new GenericType<List<Account>>(){});
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Account>();
		}
	}



	public static Booking getFirstBooking(String where){
		ClientRequest request = new ClientRequest(BOOKING_URL + "/first");
		request.body("application/xml", where);
		try {
			ClientResponse<Booking> response = request.post(Booking.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static boolean deleteBooking(Booking obj){
		ClientRequest request = new ClientRequest(BOOKING_URL + "/delete");

		//IMPORTANT: put the object into request
		request.body("application/xml", obj);

		try {
			ClientResponse<Boolean> response = request.post(Boolean.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public static boolean saveBooking(Booking obj){
		ClientRequest request = new ClientRequest(BOOKING_URL + "/save");

		//IMPORTANT: put the object into request
		request.body("application/xml", obj);

		try {
			ClientResponse<Boolean> response = request.post(Boolean.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}


	public static List<Booking> getListBooking(String where){
		ClientRequest request = new ClientRequest(BOOKING_URL + "/list");
		request.body("application/xml", where);
		try {
			ClientResponse<List<Booking>> response = request.post(new GenericType<List<Booking>>(){});
			return response.getEntity(new GenericType<List<Booking>>(){});
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Booking>();
		}
	}


	public static Flight getFirstFlight(String where){
		ClientRequest request = new ClientRequest(FLIGHT_URL + "/first");
		request.body("application/xml", where);
		try {
			ClientResponse<Flight> response = request.post(Flight.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static List<Flight> getListFlight(String where){
		ClientRequest request = new ClientRequest(FLIGHT_URL + "/list");
		request.body("application/xml", where);
		try {
			ClientResponse<List<Flight>> response = request.post(new GenericType<List<Flight>>(){});
			return response.getEntity(new GenericType<List<Flight>>(){});
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Flight>();
		}
	}

	public static Aircraft getFirstAircraft(String where){
		ClientRequest request = new ClientRequest(AIRCRAFT_URL + "/first");
		request.body("application/xml", where);
		try {
			ClientResponse<Aircraft> response = request.post(Aircraft.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static List<Aircraft> getListAircraft(String where){
		ClientRequest request = new ClientRequest(AIRCRAFT_URL + "/list");
		request.body("application/xml", where);
		try {
			ClientResponse<List<Aircraft>> response = request.post(new GenericType<List<Aircraft>>(){});
			return response.getEntity(new GenericType<List<Aircraft>>(){});
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Aircraft>();
		}
	}
}
